// pages/lawlist/lawlist.js
const config = require('../../config.js')
const http = require('../../http.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isRefresh:false,
    lawData:[],
    meetingMsg:null,
    roomId:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.roomId) {
      this.setData({
        roomId: options.roomId
      })
    }
    let meetingData = wx.getStorageSync('meetingData') 
    if(meetingData){ 
      this.setData({ 
        meetingMsg:meetingData 
      }) 
    } 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getMeetingLaw(this.data.roomId)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  toRecord:function(e){
    let pos = e.currentTarget.dataset.pos
    wx.setStorageSync('lawItem', this.data.lawData[pos])
    wx.navigateTo({
      url: '../record/record?roomId='+ this.data.roomId+"&itemId="+this.data.lawData[pos].id
    })
  },
  bindscrolltolower:function(){
    console.log("bindscrolltolower")
  },

  bindrefresherrefresh:function(){
    console.log("bindrefresherrefresh")
    this.setData({
      isRefresh:true
    })
    this.getMeetingLaw(this.data.roomId)
  },

  getMeetingLaw:function(rId){
    var that = this
    http.request(
      config.meetings + "/" + rId,
      'POST', null,
      function (res) {
        if (res.data != null && res.data.length>0) {
          let finishedCount = 0
          for(var i = 0;i< res.data.length;i++){
            if(res.data[i].resUrl != null && res.data[i].resUrl !=""){
              res.data[i].tag = true
              finishedCount++
            }else{
              res.data[i].tag = false
            }
          }

          that.data.meetingMsg.totalFinished = finishedCount
          that.setData({
            lawData:res.data,
            meetingMsg: that.data.meetingMsg
          })
        }

        if(that.data.isRefresh){
          that.setData({
            isRefresh:false
          })
        }
      },
      function (err) {
        wx.showToast({
          title: err,
          icon: 'none',
        })
      }
    )
  }
})